import html2Canvas from 'html2canvas';
import domtoimage from 'dom-to-image'

export function dom2img(node) {
  return domtoimage.toPng(node, { cacheBust: false, 
    width: 700,
    height: 1570,
    style:{
      background: '#0069ff',
      zoom: 1,
      borderRadius: 100,
      margin: 'auto',
      position: 'relative',
      marginTop:20,
    }
  })
    .then(function(dataUrl) {
      return base64ToFile(dataUrl)
    })
    .catch(function(error) {
      console.error('oops, dom2img went wrong!', error);
    });
} 



const getJpeg = dom => {
  return new Promise(resolve => {
    html2Canvas(dom).then(canvas => {
      const jpeg = canvas.toDataURL('image/jpeg', 1.0);
      // resolve(new Blob([jpeg]));
      console.log(jpeg)
      resolve(base64ToFile(jpeg));
    });
  });
};
// base64转化为file文件
const base64ToFile = urlData => {
  const arr = urlData.split(',');
  const mime = arr[0].match(/:(.*?);/)[1];
  const bytes = atob(arr[1]);
  let n = bytes.length;
  const ia = new Uint8Array(n);
  while (n--) {
    ia[n] = bytes.charCodeAt(n);
  }
  return new File([ia], 'jpeg', { type: mime });
};

export default getJpeg;
